package com.gxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.entity.*;
import com.gxa.mapper.MenuMapper;
import com.gxa.mapper.SysRoleMapper;
import com.gxa.mapper.SysUserMapper;
import com.gxa.mapper.UserMapper;
import com.gxa.security.entity.SecurityUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

//自定义UserDetailsService实例对象，改用自己定义的。
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       final int STATUS=1;
        //根据用户查询用户权限列表
//        Sys_User sys_user = sysUserMapper.findSysUserByUsername(username);
        QueryWrapper<AppUser> wrapper = new QueryWrapper<>();
        wrapper.eq("name",username).eq("status",STATUS);
        AppUser user = userMapper.selectOne(wrapper);
        //判断
        if(user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
//        //RBAC基于角色的权限控制
//        //根据用户id查询对应角色信息
//        List<Sys_Role> roleList = sysRoleMapper.findSysRoleByUserId(sys_user.getId());
//        //角色用Set来装，目的是去重，还有info对象中需要Set格式
//        Set<String> roleSets = new HashSet<>();
//        //获取角色ID
//        List<Integer> role_ids = new ArrayList<>();
//        for (Sys_Role role:roleList) {
//            roleSets.add(role.getTitle());
//            role_ids.add(role.getId());
//        }
        //根据账户id查询对应的角色
        //根据对应的角色id查询对应权限
//        Set<String> sys_menuSets = new HashSet<>();
//        List<Sys_Menu> sys_menus = sysRoleMapper.findMenuListByRoleId(role_ids);
//        for (Sys_Menu sm:sys_menus) {
//            sys_menuSets.add(sm.getTitle());
//        }
        QueryWrapper<AppMenu> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id",user.getMid());
        List<AppMenu> menus = menuMapper.selectList(wrapper1);
        System.out.println(menus);
        Set<String> set = new HashSet<>();
        for (AppMenu menu:menus) {
            set.add(menu.getMenu());
        }
        System.out.println(set);
        //将查询的账户与权限塞给SecurityUser对象
        SecurityUser securityUser = new SecurityUser();
//        SysUser sysUser = new SysUser();
//        BeanUtils.copyProperties(sys_user,sysUser);
        //塞对象
        securityUser.setCurrentUserInfo(user);
        //塞权限
        securityUser.setPermissionValueList(set);
        return securityUser;
    }
}
